#include "dboperation.h"

DBOperation* DBOperation::instance = new DBOperation();

DBOperation::DBOperation():
    db(QSqlDatabase::addDatabase("QODBC")), query(QSqlQuery(db))
{
    db.setHostName("127.0.0.1");//172.27.60.59
    db.setPort(3306);
    db.setUserName("root");
    db.setPassword("123456");
    db.setDatabaseName("test");
}

bool DBOperation::open() {

    bool ok = db.open();
    if (ok){
        qDebug() << "connect success";
    }
    else {
        qDebug() << "Error" << db.lastError();
    }
    return ok;
}

void DBOperation::close() {
    db.close();
}

bool DBOperation::operate(QString cmd) {
    bool res = query.exec(cmd);
    if (!res) {
        qDebug() << query.lastError().text();
    }
    return res;
}

bool DBOperation::getNext() {
    return query.next();
}
